package com.lzx.iteam.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class ButtonGridLinearLayout extends LinearLayout{

    private int finalWidth;
    private int finalHeight;
    
    public ButtonGridLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
            int tag;
            int orientation = getOrientation();
            if(orientation==VERTICAL){
                tag = 1;
            }else{
                tag = 0;
            }
            ButtonGridLayout bgl = (ButtonGridLayout) getChildAt(tag);
            LinearLayout ll = (LinearLayout) getChildAt(tag+1);
            int width1 = bgl.getMeasuredWidth();
            int height1 = bgl.getMeasuredHeight();
            int width2 = ll.getMeasuredWidth();
            int height2 = ll.getMeasuredHeight();

            int width = getMeasuredWidth();
            int height = getMeasuredHeight();
            int columns = bgl.getColumns();
            int rows = bgl.getRows();

            int temp;
            int widthSpec;
            int heightSpec;
            if(orientation==VERTICAL){
                temp = height1%rows;
                if(temp!=0){
                    height1+=rows-temp;
                    height2-=rows-temp;
                }
                temp = width1%columns;
                if(temp!=0){
                    width1+=columns-temp;
                }
            /*if(width1>width){
                finalWidth = MeasureSpec.makeMeasureSpec(width1, MeasureSpec.EXACTLY);
                finalHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            }else{*/
                widthSpec = MeasureSpec.makeMeasureSpec(width1, MeasureSpec.EXACTLY);
                heightSpec = MeasureSpec.makeMeasureSpec(height1, MeasureSpec.EXACTLY);
                getChildAt(tag).measure(widthSpec, heightSpec);

                widthSpec = MeasureSpec.makeMeasureSpec(width1, MeasureSpec.EXACTLY);
                heightSpec = MeasureSpec.makeMeasureSpec(height2, MeasureSpec.EXACTLY);
                getChildAt(tag+1).measure(widthSpec, heightSpec);
           /* }*/
            }else{
                temp = width1%columns;
                if(temp!=0){
                    width1+=columns-temp;
                    width2-=columns-temp;
                }
                temp = height1%rows;
                if(temp!=0){
                    height1+=rows-temp;
                }
            /*if(height1>height){
                finalWidth = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
                finalHeight = MeasureSpec.makeMeasureSpec(height1, MeasureSpec.EXACTLY);
            }else{*/
                widthSpec = MeasureSpec.makeMeasureSpec(width1, MeasureSpec.EXACTLY);
                heightSpec = MeasureSpec.makeMeasureSpec(height1, MeasureSpec.EXACTLY);
                getChildAt(tag).measure(widthSpec, heightSpec);

                widthSpec = MeasureSpec.makeMeasureSpec(width2, MeasureSpec.EXACTLY);
                heightSpec = MeasureSpec.makeMeasureSpec(height1, MeasureSpec.EXACTLY);
                getChildAt(tag+1).measure(widthSpec, heightSpec);
            /*}*/
            }
        super.onLayout(changed, l, t, r, b);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       /* if(finalHeight!=0&&finalWidth!=0){
            widthMeasureSpec = finalWidth;
            heightMeasureSpec = finalHeight;
        }*/
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        
    }

}
